/**
    java String的split函数里面要填入一个正则表达式  “.” 要用 “[.]”或"\\."
*/
package com.zjut.an.util.tools;

import javax.servlet.http.HttpServletRequest;

public class IPUtils {
    public static int[] IPsplit(String ip) {
        return IPsplit(ip,false);
    }
    /**
     * reutrn a int array with 4 size
     * 
     * @param ip
     * @return
     */
 
 
     public static int[] IPsplit(String ip,boolean max) {
        int[] inum = new int[4];
        try {
            String[] nums = ip.split("[.]");//String[] nums = ip.split("\\.");
            int len =nums.length;
            for (int i = 0; i < len; i++) {
                inum[i] = Integer.parseInt(nums[i]);
            }
            if(max) {//if max ,fill 256 to int[]
                if(len<inum.length) {
                    for (int j = len; j < inum.length; j++) {
                        inum[j]=256;
                    }
                }
            }
        } catch (Exception e) {
        }
        return inum;
    }

     /**
      * return myIP is between school ip area.
      * 
      * @param myIP
      * @return
      */
     public static boolean isBetween(String myIP) {
     	if (isBetween(myIP, "10.1.0.0", "10.255.255.255") || isBetween(myIP, "172.16.0.0", "172.16.8.255")|| isBetween(myIP, "172.16.10.0", "172.31.10.0")||isBetween(myIP, "210.32.112.0", "210.32.119.255")||isBetween(myIP, "210.32.200.0", "210.32.207.255")||isBetween(myIP, "115.200.0.0", "115.200.103.255")||isBetween(myIP, "127.0.0.0", "127.0.0.255")||isBetween(myIP, "172.16.128.0", "172.16.255.255")) {
     		//              / 教学区                       /                     /*内网                                  排除反响代理                                      */                 /*学校教科网                                                                                   */                   /*学生生活区                         */           /*回环地址                           */                  /*之江                            */
     		return true; 
     	} else {
     		return false;
     	}
     }

    /**
     * return myIP is between ip1~ip2
     * 
     * @param myIP
     * @param ip1
     * @param ip2
     * @return
     */
    public static boolean isBetween(String myIP, String ip1, String ip2) {
        if (myIP == null) {
            return true;
        }
        if (null == ip1) {
            ip1 = "0.0.0.0";
        }
        if (null == ip2) {
            ip2 = "256.0.0.0";
        }
        int[] myIPs = IPsplit(myIP);

        return arraySub(myIPs, IPsplit(ip2,true)) && arraySub(IPsplit(ip1), myIPs);
    }

    private static boolean arraySub(int[] a, int[] b) {
        for (int i = 0; i < a.length; i++) {
            if (b[i] - a[i] > 0) {
                return true;
            } else if (b[i] - a[i] < 0) {
                return false;
            } else {
                if(i+1==a.length) {//check is end
                    return true;
                }else {
                    continue;
                }
            }
        }
        return false;
    }

    public static String getIpAddr(HttpServletRequest request) {  
        String ip = request.getHeader("X-Forwarded-For");  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("Proxy-Client-IP");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("WL-Proxy-Client-IP");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("HTTP_CLIENT_IP");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");  
        }  
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
            ip = request.getRemoteAddr();  
        }  
        return ip;  
    } 
}
